from urllib import request
import re
import time
import itchat

lists = []
while True:
    #获取html函数
    def gethtml():
        global lists
        base_url = "http://live.sina.com.cn/zt/f/v/finance/globalnews1"
        response = request.urlopen(base_url)
        html = response.read().decode()
        reg = re.compile(r'<p class="bd_i_time_c">(.+?)</.+?<p class="bd_i_txt_c">(.+?)</p>',re.S)
        #取出前五条数据循环
        result = reg.search(html)
        t = result.group(1)
        data = result.group(2)


        #判断列表是否有当前获取的文章 没有则加入列表 推送消息
        if data not in lists:
            lists.clear()
            lists.append(data)
            #将新推送的文章发送给好友
            msg = '你好！北京时间:'+t+"\n"+'python小鑫为您推送最新消息:'+"\n"+data
            login(msg)




    #登陆微信函数
    def login(msg):
        print(msg)
        itchat.auto_login(hotReload=True)
        friend = itchat.get_friends()
        for i in friend:
            if i["RemarkName"] == "孟帅伟":
                itchat.send(msg, toUserName=i["UserName"])



    gethtml()
    time.sleep(3)

